home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11404 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.1 KB

  1. Path: sol.caps.maine.edu!bensh
  2. From: bensh@gandalf.UMCS.Maine.EDU (Shawn Benn)
  3. Newsgroups: comp.lang.c
  4. Subject: Problem with pointers
  5. Date: 23 Mar 1996 21:50:27 GMT
  6. Organization: University of Maine, Department of Computer Science
  7. Distribution: world
  8. Message-ID: <4j1rn3$19du@sol.caps.maine.edu>
  9. NNTP-Posting-Host: gandalf.umcs.maine.edu
  10.  
  11.  
  12. Hi!  I have been trying to debug a problem for the past week and have gotten
  13. absolutely nowhere.
  14.  
  15. Here's the problem:
  16.  
  17. I call a function where its supposed to load several nodes into a linked list.
  18. The linked list has the correct number of elements during the function where
  19. it initializes and adds all the elements.  So I assume this is working correctly.
  20. The problem exists when I try to access the data in the linked list back in
  21. the calling function.
  22.  
  23. Here's the code in question for the function call (ignore the ... as this means
  24. there are other unimportant (to the problem) parameters to that function):
  25.  
  26. typedef struct classnode* Class;
  27.  
  28. Class cls;
  29. LoadClass(..., &cls, ...);
  30. printf("%d\n", GetCount(cls));
  31.  
  32. This printf prints 0.
  33.  
  34. Here's the code from LoadClass:
  35.  
  36. void LoadClass(..., Class* cls, ...)
  37. {
  38.   ...
  39.     for (i=0; i<(int)recs; ++i) {
  40.       ReadStudent(&s, fil, i);   /* Read a student and add to the list */
  41.       if (!IsEmpty(*cls)) {
  42.         s1 = GetHead(cls);
  43.         ptr = &cls;
  44.         while ((GetName(&s) > GetName(&s1)) && (ptr->next != NULL)) {
  45.           ptr = ptr->next;
  46.           s1 = ptr->s;
  47.         }
  48.         if ((GetName(&s) > GetName(&s1)) || (ptr->next == NULL))
  49.           AddTail(&cls, s);
  50.         else
  51.           InsertAt(&cls, s, GetIndex(&s1));
  52.       }
  53.       else
  54.         AddHead(&cls, s);
  55.     }
  56.     printf("%d\n", GetCount(&cls));
  57.   }
  58. }
  59.  
  60. This printf prints 13 (the actual value for the number of elements in the list
  61. I am loading).  I have also debugged the above routine and went step by step
  62. through the above loop.  Everything seems to be correctly done.  I know the 
  63. problem exists in the call to LoadClass, but I do not know the solution.  I am
  64. afraid I do not completely understand how C pointers work.
  65.  
  66. Thanks for any help.
  67.  
  68. Shawn Benn
  69. University of Maine
  70.